﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web.Http;
using RBS.Core.Logs;
using RBS.Web.API.Models;
using JsonData = RBS.Common.JsonEntity.JsonData;

namespace RBS.Web.API.Areas.Hotel.Controllers
{
    /// <summary>
    /// 图片下载
    /// </summary>
    public class DownLoadController : ApiController
    {
        private   readonly ApiContext _db = new ApiContext(); 

        /// <summary>
        /// 图片下载
        /// </summary>
        /// <param name="id">图片ID</param>
        /// <param name="type">hotel：获取酒店图片;Cookbook:获取菜谱图片</param>
        /// <returns>二进制流</returns>
        public HttpResponseMessage GetImgByte(int id, string type)
        {
            var jsonData = new JsonData();
            if (type.ToUpper() == "HOTEL")
            {  
                var imgtemp = GetHotelImg(id);
                var result = new HttpResponseMessage { Content = new ByteArrayContent(imgtemp) };
                return result;  
            }
            if (type.ToUpper() == "COOKBOOK")
            { 
                var imgtemp = GetCookbookImg(id);
                var result = new HttpResponseMessage { Content = new ByteArrayContent(imgtemp) };
                return result; 
            } 
            return null;
        }

        /// <summary>
        /// 根据酒店图片ID获取图片
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private byte[] GetHotelImg(int id)
        {
            var jsonData = new JsonData();
            var imgpach = RBS.Common.ConfigHelper.GetConfigString("imgpach");
            var hHotelImg = _db.VHotel.Where(i => i.Id == id).OrderBy(i => i.Id).FirstOrDefault();
            //var hHotelImg = _db.HotelImg.Where(i => i.Id == id).OrderBy(i => i.Id).FirstOrDefault();
            if (hHotelImg != null)
            {
                string imgurl = imgpach +
                                hHotelImg.Img;
                jsonData.Success = true;
                return  GetImageByte(imgurl);
            } 
            return null;
        } 
        /// <summary>
        /// 获取菜谱图片
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private byte[] GetCookbookImg(int id)
        {
            var jsonData = new JsonData();
            var imgpach = RBS.Common.ConfigHelper.GetConfigString("imgpach");
            var hHotelImg = _db.HotelCookbook.Where(i => i.Id == id).OrderBy(i => i.Id).FirstOrDefault();
            if (hHotelImg != null)
            {
                string imgurl = imgpach +
                                hHotelImg.DishImages;
                jsonData.Success = true;
                return  GetImageByte(imgurl);
            } 
            return null;
        }

        /// <summary>
        /// 根据图片路径返回字节流
        /// </summary>
        /// <param name="imagePath"></param>
        /// <returns></returns>
        private   byte[] GetImageByte(string imagePath)
        {
            FileStream files = new FileStream(imagePath, FileMode.Open);
            byte[] imgByte = new byte[files.Length];
            files.Read(imgByte, 0, imgByte.Length);
            files.Close();
            return imgByte;
        }
    }
}
